package cn.spike.pet.service.impl;

import cn.spike.basic.service.IBaiduAiAuditService;
import cn.spike.basic.service.impl.BaseServiceImpl;
import cn.spike.basic.util.AjaxResult;
import cn.spike.pet.domain.Pet;
import cn.spike.pet.domain.PetDetail;
import cn.spike.pet.domain.PetOnlineAuditLog;
import cn.spike.pet.mapper.PetDetailMapper;
import cn.spike.pet.mapper.PetMapper;
import cn.spike.pet.mapper.PetOnlineAuditLogMapper;
import cn.spike.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    @Transactional
    public void add(Pet pet) {
        //添加宠物
        super.add(pet);
        //获取到宠物详情信息并设置petId
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        //添加宠物详情信息
        petDetailMapper.add(petDetail);
    }


    @Override
    @Transactional
    public void delete(Long id) {
        //删除宠物详情信息
        petDetailMapper.deleteByPetId(id);

        //删除宠物信息
        super.delete(id);
    }


    @Override
    @Transactional
    public void update(Pet pet) {
        //修改宠物信息
        super.update(pet);
        //获取前端传过来的宠物详情信息
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPetId(pet.getId());
        petDetailMapper.updateByPetId(petDetail);
    }


    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 宠物上架的业务
     *
     * @param ids
     */
    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            Boolean boo1 = baiduAiAuditService.textAudit(pet.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(pet.getResources());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            if (boo1 && boo2) {
                //更改宠物状态
                petMapper.onsale(id, new Date());
                log.setPetId(id);
                log.setNote("审核通过");
                log.setState(1);
            } else {
                log.setPetId(id);
                log.setNote("审核失败");
                log.setState(0);
                return AjaxResult.me().setSuccess(false).setMsg(pet.getName() + "审核失败");
            }
            //将审核日志添加到数据库
            petOnlineAuditLogMapper.add(log);
        }
        return new AjaxResult();
    }


    /**
     * 宠物下架的业务
     *
     * @param ids
     */
    @Override
    @Transactional
    public AjaxResult offsale(List<Long> ids) {
        petMapper.offsale(ids, new Date());
        return new AjaxResult();
    }
}
